var FOLDER_INVOICES_ID = "0B0u92JJBtL_9c3lFWllKMmRFMUU";

/**
 * Creates an invoice document from a booking.
 */
function createInvoice_(booking) {
  var template = SpreadsheetApp.openById("1lSTkfA5S6wBM19mVmiX_OFQGF9L92NpwsobD24hhgjM");
  
  // Increment invoice number
  var rngInvoiceNumber = template.getRangeByName("InvoiceNumber");
  var invoiceNumber = rngInvoiceNumber.getValue() + 1
  rngInvoiceNumber.setValue(invoiceNumber);
  
  // Copy template to a new document
  var invoiceFileName = "invoice_" + invoiceNumber + "_" + booking.eventName;
  var invoice = template.copy(invoiceFileName);
  
  // Move to invoices folder
  var invFile = DriveApp.getFileById(invoice.getId());
  if(invFile != null) {
    // Add to invoices folder
    var invFolder = DriveApp.getFolderById(FOLDER_INVOICES_ID);
    invFolder.addFile(invFile);
    // Remove from invoices folder
    var rootFolder = DriveApp.getRootFolder();
    rootFolder.removeFile(invFile);
  }
  
  // Set invoice date
  var rngInvoiceDate = invoice.getRangeByName("InvoiceDate");
  rngInvoiceDate.setValue(booking.timestamp);
  
  // Set invoicee details
  invoice.getRangeByName("AttentionOfName").setValue(booking.contact.name);
  invoice.getRangeByName("AttentionOfOrg").setValue(booking.organisation.name);
  invoice.getRangeByName("AttentionOfEmail").setValue(booking.contact.email);
  
  // Set event details
  invoice.getRangeByName("EventDescription").setValue("Invoice for " + getEventDescription_(booking));
  
  // Add invoice lines
  var rngInvoiceItems = invoice.getRangeByName("InvoiceItems");
  for(i = booking.invoice.lines.length - 1; i >= 0; i --) {
    var iLine = booking.invoice.lines[i];

    invoice.insertRowBefore(rngInvoiceItems.getRow());
    rngInvoiceItems.setValues([[iLine.lineID, iLine.description, iLine.unitCost, iLine.quantity, iLine.totalCost]]);
  }
  
  // Set total
  invoice.getRangeByName("GrandTotal").setValue(booking.invoice.total);
  
  // Get payments
  var rngPaymentItems = invoice.getRangeByName("PaymentItems");
  
  // Show deposit line if deposit is required, otherwise just show balance payment.
  if(booking.invoice.deposit.amount > 0) {
    // Set deposit amount
    rngPaymentItems.setFontColor("#ff0000").setValues([["", "Deposit", "NOW DUE", "", booking.invoice.deposit.amount]]);
    
    // Insert a row and set balance amount
    invoice.insertRowAfter(rngPaymentItems.getRow());
    rngPaymentItems.offset(1, 0, 1, 5).setFontColor("#000000").setValues([["", "Balance", "Due", booking.invoice.balance.dateDue, booking.invoice.balance.amount]]);
    
    // Hide payment due in full notice
    invoice.hideRow(invoice.getRangeByName("PaymentNoticeFull"));
  } else {
    // Set payment value to total balance
    rngPaymentItems.setFontColor("#ff0000").setValues([["","Balance", "NOW DUE", "", booking.invoice.balance.amount]]);
    
    // Hide deposit due notice
    invoice.hideRow(invoice.getRangeByName("PaymentNoticeDeposit"));
  }
  
  // Return invoice details
  return {
      "invoiceID"  : invoice.getId()
    , "invoiceAmt" : booking.invoice.total
  };
}
 